package org.top10.parser;

import java.util.ArrayList;
import java.util.List;

import org.top10.entity.CollectEntity;
import org.top10.entity.Reading;
import org.top10.executor.ParseSource;
import org.top10.parser.LuoboParser;
import org.top10.parser.LuoboTask;
import org.top10.parser.TOPParserImpl;
import org.top10.parser.Task;
import org.top10.util.TOP10Global;
import org.w3c.dom.Node;

public class DocinTask extends Task{
	private static final int webSiteID = TOP10Global.WEB_SITE_ID_DOCIN;// 对应网站ID
	private static final String ENCoding =TOP10Global.Encoding_UTF8;// 对应网站ID
	
	public DocinTask() {
		super(webSiteID, ENCoding);
	}

	public void loadParser() {
		PARSER.add(new DocinParser("月排行","http://www.docin.com/list-0-0-1-3-1-1.html",null));
		PARSER.add(new DocinParser("周排行","http://www.docin.com/list-0-0-1-2-1-1.html",null));
		PARSER.add(new DocinParser("日排行","http://www.docin.com/list-0-0-1-1-1-1.html",null));
	}
	public static void main(String arg[]) throws Exception{
		Task t=new DocinTask();
		t.loadParser();
		t.PARSER.get(2).dealText(TOP10Global.Encoding_UTF8);
	
	}
	
}
 class DocinParser  extends TOPParserImpl{
	private String sign;
	private  final static String BaseUrl="http://www.docin.com";
	public DocinParser(String c,  String u,String sign) {
		super(c, TOP10Global.TYPE_Reading, u);
		this.sign=sign;
	}
	public  List<CollectEntity> parse(ParseSource doc) throws Exception {
		Node body = doc.getDocSource().getElementsByTagName("body").item(0);
		Node node=null;
		node=getNodeByAttr("div","class","doc-list doc-mark",body);
		ArrayList<Node> as = new ArrayList<Node>();
		getAllTagsByParent(node, "a", as);
		List<CollectEntity> list = new ArrayList<CollectEntity>();
		int rank=1;
		for (int i = 1; i < as.size(); i=i+3) {
			Node tr=as.get(i);
			CollectEntity v =build(tr,rank++);
			System.out.println(v);
			list.add(v);
			if(i>=27)break;
		}
		return list;
	}

	public  void build1(Node node, int rank,CollectEntity v) throws Exception {
		
	}
	public CollectEntity build(Node node, int rank) throws Exception {
		CollectEntity v=new Reading();
		v.setRank(rank);
		String name=getAttrValueByTag(node,"alt");
		if(name==null||name.trim().equals("")){
			v.setName(node.getTextContent());
		}else{
			v.setName(name);
		}
		String url=getAttrValueByTag(node,"href");
		v.setLinkUrl(BaseUrl+url);
		return v;
	}
	
}